<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <script>
         // Generator  Promise  async  1.解决回调地域  2.使得异步操作显得更加的方便
         // 作用：使得异步操作更加方便
         // 基本操作 async它会返回一个Promise对象 then catch
         // async是Generator的一个语法糖
         async function f(){
             // await不能单独使用，必须配合async
             //return await 'hello async';
             let s = await 'hello world';
             let data = await s.split('');
             return data;
         }
         // 如果async函数中有多个await 那么then函数会等待所有的await指令 运行完的结果 才去执行
         f().then(v => {console.log(v)}).catch(e=>console.log(e));

         async function f2(){
             // throw new Error('出错了');
             try{
                 await Promise.reject('出错了');
             } catch (error){

             }
             return await Promise.resolve('hello');
         }
         f2().then(v => console.log(v)).catch(e => console.log(e));


         async function fetchData() {
             try {
                 let response = await fetch('https://api.example.com/data');
                 if (!response.ok) throw new Error('Network response was not ok');
                 let data = await response.json();
                 console.log(data);
             } catch (error) {
                 console.log('Error:', error);
             }
         }

         fetchData();
    </script>
</body>
</html>